﻿const cookieHelper = {
    setItem: (name, value) => {
        var Days = 7;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
    },
    getItem: (name) => {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

        if (arr = document.cookie.match(reg))

            return unescape(arr[2]);
        else
            return null;
    },
    removeItem: (name) => {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = getItem(name);
        if (cval != null)
            document.cookie = name + "=" + cval + ";path=/;expires=" + exp.toGMTString();
    },
    clear: () => {
        var keys = document.cookie.match(/[^ =;]+(?==)/g);
        if (keys) {
            for (var i = keys.length; i--;) {
                document.cookie =
                    keys[i] +
                    "=0;path=/;expires=" +
                    new Date(0).toUTCString() +
                    ";max-age=0";
            }
        }
    }
}

const utils = {
    config: {
        storage: cookieHelper,
        tokenSign: 'authorization_token' // token名字
    },
    getQueryVariable: (variable) => {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
        }
        return (false);
    }
}